using System;
using System.Collections;
using System.Reflection;
using System.Text;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;
using InfoJetSoft.Service.ManagedCode;

namespace InfoJetSoft.Service.Worker
{
	/// <summary>
	/// Summary description for RootAssemblyLoadWorker.
	/// </summary>
	class ManagedCodeOnLoadWorker
	{
		
		public static void OnLoad(Form form)
		{
			if( form.Manifest.ManagedAssembly == null )
			{
				return;
			}

			//CID 0217 //2009.01.30 //Begin
			if( form.Manifest.ManagedAssembly.ModelType != null )
			{
				OnManagedCodeLoad( form );
			}
			else
			{
				OnProxyCodeLoad( form );
			}
			//CID 0217 //2009.01.30 //End
		}

		//CID 0217 //2009.01.30 //Begin
		private static void OnProxyCodeLoad( Form form )
		{
			MethodInfo loadingMethod = ProxyCodeUtil.findMethod( form.Manifest.ManagedAssembly.MainType, "Loading" );
			if( loadingMethod == null )
			{
				return;
            }

            Hashtable parameters = new Hashtable();
            object proxyObject = ProxyCodeUtil.CreateProxyObject(form, parameters);

			try
			{
				//CID 0659 //2010.09.16 //Begin
                if (loadingMethod.GetParameters().Length == 1)
                {
                    loadingMethod.Invoke(null, new object[] { proxyObject });
                }
                else
                {
                    loadingMethod.Invoke(null, new object[] { proxyObject, null });
                }
                //CID 0659 //2010.09.16 //End
			}
			catch( Exception e )
			{
                FormUtil.addInnerExceptionMassage(form, loadingMethod.Name, e);
			}
		}
		//CID 0217 //2009.01.30 //End

		private static void OnManagedCodeLoad( Form form )
		{
			ManagedCodeUtil.createModel( form );
			object model = form.ManagedCodeModel;

			MethodInfo onLoad = ManagedCodeUtil.findMethod( model, Microsoft.Office.Interop.InfoPath.SemiTrust.InfoPathEventType.OnLoad );
			if( onLoad == null )
			{
				return;
			}

			XDocument xdoc = new XDocument( form );
			Application app = new Application( form );
			DocReturnEvent docReturnEvent = new DocReturnEvent( xdoc );

            //CID 0702 //2010.11.29 //Begin
            SDSMonitor sdsMonitor = new SDSMonitor(form);
            //CID 0702 //2010.11.29 //End
            try
            {
                MethodInfo startup = ManagedCodeUtil.findMethod(model, "_Startup");
                startup.Invoke(model, new object[] { app, xdoc });

                onLoad.Invoke(model, new object[] { docReturnEvent });

                MethodInfo shutdown = ManagedCodeUtil.findMethod(model, "_Shutdown");
                shutdown.Invoke(model, new object[] { });
            }
            catch (Exception e)
            {
                FormUtil.addInnerExceptionMassage(form, onLoad.Name, e);
            }
            finally
            {
                //CID 0702 //2010.11.29 //Begin
                sdsMonitor.Stop();
                //CID 0702 //2010.11.29 //End
            }
		}

	}
}
